package com.dckeji.shiplock.webapp.controller.rest;

import com.alibaba.fastjson.JSONObject;
import com.dckeji.shiplock.framework.constants.SystemConstants;
import com.dckeji.shiplock.framework.util.StringUtil;
import com.dckeji.shiplock.webapp.controller.BaseController;
import com.dckeji.shiplock.webapp.pojo.LogicRsult;
import com.dckeji.shiplock.webapp.pojo.Pay;
import com.dckeji.shiplock.webapp.service.IPayService;
import com.dckeji.shiplock.webapp.util.ReturnMsgUtil;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * User: TianWei
 * Date: 2017/8/8
 * Time: 10:32
 * 缴费上传
 */
@RequestMapping("/rest/pay/")
@RestController
public class PayController extends BaseController {

    @Value("${qiniu.ak}")
    String accessKey;
    @Value("${qiniu.sk}")
    String secretKey;
    @Value("${qiniu.bucketName}")
    String bucketName;
    @Value("${qiniu.domain}")
    String domain;

    @Autowired
    IPayService payService;

    @RequestMapping("getToken")
    public LogicRsult getUpLoadToken() {
        LogicRsult logicRsult = new LogicRsult();
        String upToken = Auth.create(accessKey, secretKey).uploadToken(bucketName);
        JSONObject object = new JSONObject();
        object.put("upToken", upToken);
        object.put("domain", domain);
        logicRsult.setObj(object);
        logicRsult.setResultCode(SystemConstants.ResultCode.SUCCESS);
        logicRsult.setResultMsg(SystemConstants.ResultMsg.SUCCESS);
        return logicRsult;
    }

    @RequestMapping("commit")
    public LogicRsult commitPayVoucher(@RequestBody Pay pay) {
        LogicRsult logicRsult = new LogicRsult();
        if (valida(pay)) {
            int row = payService.commitPayVoucher(pay.getShipNo(), pay.getVouCherUrl());
            if (row > 0) {
                logicRsult.setResultCode(SystemConstants.ResultCode.SUCCESS);
                logicRsult.setResultMsg(SystemConstants.ResultMsg.SUCCESS);

            } else {
                logicRsult.setResultCode(SystemConstants.ResultCode.FIAL);
                logicRsult.setResultMsg(SystemConstants.ErrorMsg.SYS_ERROR);
            }
        } else {
            logicRsult.setResultCode(SystemConstants.ResultCode.FIAL);
            logicRsult.setResultMsg(SystemConstants.ErrorMsg.VALIDA);
        }

        return new LogicRsult();
    }

    @RequestMapping("query")
    public LogicRsult query(@RequestBody Pay pay) {
        LogicRsult logicRsult = new LogicRsult();
        if (valida(pay)) {
            List<Pay> query = payService.query(pay.getShipNo(), pay.getIsExamine());
            if (!StringUtil.isNull(query) && query.size() > 0) {
                logicRsult.setObj(query);
                logicRsult.setResultCode(SystemConstants.ResultCode.SUCCESS);
                logicRsult.setResultMsg(SystemConstants.ResultMsg.SUCCESS);
            } else {
                logicRsult.setResultCode(SystemConstants.ResultCode.FIAL);
                logicRsult.setResultMsg(SystemConstants.ErrorMsg.NO_DATA);
            }
        }
        return logicRsult;
    }

    @RequestMapping("queryAll")
    public LogicRsult queryAll() {
        List<Pay> pays = payService.queryAll();
        if (null != pays && pays.size() > 0) {
            return ReturnMsgUtil.success(pays);
        } else {
            return ReturnMsgUtil.notData();
        }

    }


}
